package com.ltv.xiangqiplugin.views;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import com.ltv.xiangqiplugin.models.Languages;
import com.ltv.xiangqiplugin.models.Messages;
import com.ltv.xiangqiplugin.models.MessagesObserver;

public class AboutFENView extends JDialog implements MessagesObserver {
	private static AboutFENView instance;
	
	private JTextPane fenDescEN;
	private JTextPane fenDescVI;
	
	JPanel localPanel;

	private AboutFENView() throws Exception {
		super();
		fenDescEN = new JTextPane();
		fenDescVI = new JTextPane();
		
		fenDescEN.setContentType("text/html");
		fenDescVI.setContentType("text/html");
		
		InputStream enFS = null;
		InputStreamReader enISR = null;
		BufferedReader enReader = null;
		
		StringBuffer enContent = new StringBuffer();
		enContent.append("<html>");
		enContent.append("\n");
		enContent.append("<pre>");
		
		try {
			enFS = getClass().getResourceAsStream("/messages/fen_en.txt");
			enISR = new InputStreamReader(enFS, "UTF-8");
			enReader = new BufferedReader(enISR);
			String line = enReader.readLine();
			while (line != null) {
				enContent.append(line);
				enContent.append("\n");
				line = enReader.readLine();
			}
		} finally {
			try {
				enFS.close();
			} catch (Exception e) {}
			try {
				enISR.close();
			} catch (Exception e) {}
			try {
				enReader.close();
			} catch (Exception e) {}
		}
		
		enContent.append("<img src=\"" + this.getClass().getResource("/bg/fen_example.png").toString() + "\"");
		enContent.append("\n");
		enContent.append("</pre>");
		enContent.append("\n");
		enContent.append("</html>");
		
		fenDescEN.setText(enContent.toString());
		fenDescEN.setEditable(false);
		
		InputStream viFS = null;
		InputStreamReader viISR = null;
		BufferedReader viReader = null;
		
		StringBuffer viContent = new StringBuffer();
		
		viContent.append("<html>");
		viContent.append("\n");
		viContent.append("<pre>");
		
		try {
			viFS = getClass().getResourceAsStream("/messages/fen_vi.txt");
			viISR = new InputStreamReader(viFS, "UTF-8");
			viReader = new BufferedReader(viISR);
			String line = viReader.readLine();
			while (line != null) {
				viContent.append(line);
				viContent.append("\n");
				line = viReader.readLine();
			}
		} finally {
			try {
				viFS.close();
			} catch (Exception e) {}
			try {
				viISR.close();
			} catch (Exception e) {}
			try {
				viReader.close();
			} catch (Exception e) {}
		}
		
		viContent.append("<img src=\"" + this.getClass().getResource("/bg/fen_example.png").toString() + "\"");
		viContent.append("\n");
		viContent.append("</pre>");
		viContent.append("\n");
		viContent.append("</html>");
		
		fenDescVI.setText(viContent.toString());
		fenDescVI.setEditable(false);
		
		localPanel = new JPanel(new GridLayout(1, 1, 1, 1));
		
		if (Messages.getInstance().getLang() == Languages.EN) {
			localPanel.add(new JScrollPane(fenDescEN));
		} else if (Messages.getInstance().getLang() == Languages.VI) {
			localPanel.add(new JScrollPane(fenDescVI));
		}
		
		getContentPane().add(BorderLayout.CENTER, localPanel);
		setSize(850, 600);
		requestFocus();
		
		localPanel.setPreferredSize(new Dimension(850, 600));
		
		addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
			
		});
		
		centerScreen();
		
		Messages.getInstance().addObserver(this);
	}
	
	public static AboutFENView getInstance() {
		if (instance == null) {
			try {
				instance = new AboutFENView();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return instance;
	}
	
	public static void main(String[] args) {
		AboutFENView.getInstance().setVisible(true);
	}
	
	private void centerScreen() {
		int x = (Toolkit.getDefaultToolkit().getScreenSize().width - getWidth())/2;
		int y = (Toolkit.getDefaultToolkit().getScreenSize().height - getHeight())/2;
		
		setLocation(x, y);
	}

	@Override
	public void messagesChanged() {
		localPanel.removeAll();
		if (Messages.getInstance().getLang() == Languages.EN) {
			localPanel.add(new JScrollPane(fenDescEN));
		} else if (Messages.getInstance().getLang() == Languages.VI) {
			localPanel.add(new JScrollPane(fenDescVI));
		}
		
	}

	public JPanel getLocalPanel() {
		return localPanel;
	}
	
	
}
